<?php
/*****************************************
 This file is part of the Dynamo CMS UI Toolkit
 Copyright (c) Dynamo Studios 2008

 *** NOTE ***
 Permission is hereby granted to execute this file as a
 web service under the terms of the license agreement.

 See LICENSE.txt, in the system folder.

 All other rights, other than those expressed in the license
 document, are reserved.

 ****************************************/

/**
 * @name CMS Library
 * @version 0.1 (Alvin)
 * @abstract
 * Provides widgets for content management.
 *
 * @author Sean Micklethwaite
 */


// Import editor widgets
uiLoadLibrary('editor');


class modManageContent extends cDycmsClientModule
{
	function __construct()
	{
		$cls = dyPostOrGet('$modManageContent-cls');

		if(isset($cls)){
            $this->AddChild($mgr = new cUiManager('$modManageContent',
                array('Owner', 'Summary'), array(
                new cUiButton(array(
                            'name' => '$modManageContent-action',
        					'value' => 'take'
        				),'Take Ownership', 'lock_break'))));

            //
            // Save the class in form data
            //

            $mgr->AddChild(new cUiFormData('$modManageContent-cls', $cls));

            switch($mgr->GetAction()) {
                case 'create':
                header('Location: '.dyEditorUrl($cls, NULL, true));
                break;

                case 'edit':
                header('Location: '.dyEditorUrl($cls, $mgr->GetObjectId(), true));
                break;

                case 'view':
                header('Location: '.dyViewerUrl($cls, $mgr->GetObjectId()));
                break;

                case 'sec': // Edit security
                $obj = dyLoadObject($cls, $mgr->GetObjectId());


                parent::__construct(uiGetIcon('lock_break').
                    'Take Ownership/Edit Security Levels');

                //
                // Create a blank form, saving the action in post data
                // as we have separate editors running
                //

                $mgr->CreateBlank();
                $mgr->SaveAction();

                $mgr->AddChild(new cUiOwnershipEditor($obj, array(
                    'id'=>'$modManageContent')));
    			$mgr->AddChild(new cUiTokenEditor('$modManageContent', $obj));
    			$mgr->AddChild(new cUiButton(array(
    				'name' => '$modManageContent-action',
    				'value' => ''
    			),'Done', 'accept'));



                break;


                default:    // Show table

                $class = dyLoadClassById($cls);
                parent::__construct(uiGetIcon($class->GetIcon())
                    .$class->GetName().'s');

                // Read objects
                $zz_obj = dbReadAllObjects($cls);

                $mgr->CreateForm(dbCountObjects($cls));
                
                // Override scripting
                cUiPage::GetInst()->AddScript('cms');
                cUiPage::GetInst()->AddStartScript('$modManageContent',
                	'new modManageContent($("$modManageContent"));');

                foreach($zz_obj as $zObj){
                    $owner = new cDycmsClient($zObj['$owner']);
                    $obj = $class->LoadObjectByArray($zObj);

                    $mgr->AddRow($zObj['$id'], array(
                        $owner->GetUserName(),
                        $obj->GetSummary()
                    ), array(
                        new cUiButton(array(
                            'name' => '$modManageContent-action',
        					'value' => 'view-'.$zObj['$id'],
                            'title'=>'View'
        				),'', 'magnifier'),
                        new cUiButton(array(
                            'name' => '$modManageContent-action',
        					'value' => 'sec-'.$zObj['$id'],
                            'title'=>'Take Ownership/Edit Security Levels'
        				),'', 'lock_edit')
                    ));
                }
            }
		}else{
			parent::__construct(uiGetIcon('book_edit').'Manage Content');

			$classes = dbReadAllClasses();

			$this->AddChild($f = new cUiForm(array(
				'action' => $_SERVER['REQUEST_URI'],
				'method' => 'GET'
			)));

			//
			// Filter by security check
			//
			
			foreach($classes as $i => $class){
			 	$classes[$i] = new cDycmsClass(null, null, $class);
			 	
				if(!dyCheckPriv($classes[$i]->GetManagePriv()))
			 		unset($classes[$i]);
			};
			
			if(count($classes) == 0)
				$f->AddChild(uiGetIcon('error').'You are not authorized to manage any classes of content!');
			else foreach($classes as $class) {
				$f->AddChild(new cUiButton(array(
					'name' => '$modManageContent-cls',
					'value' => $class->GetClassId(),
					'style' => 'width: 100%'
				),'Manage '.$class->GetName().'s', $class->GetIcon()));
			};
		};
	}
}

class modCreateContent extends cDycmsClientModule
{
	function __construct()
	{
		$cls = $_POST['$modCreateContent-cls'];

		if(isset($cls)){
			header('Location: '.dyEditorUrl($cls));
		}else{
			parent::__construct(uiGetIcon('book_add').'Create Content');

			$classes = dbReadAllClasses();

			$this->AddChild($f = new cUiForm(array(
				'action' => '',
				'method' => 'POST'
			)));

			foreach($classes as $class) {
			 	$class = new cDycmsClass(null, null, $class);
			 	
			 	if(!dyCheckPriv($class->GetCreatePriv()))
			 		continue;
			 	
				$f->AddChild(new cUiButton(array(
					'name' => '$modCreateContent-cls',
					'value' => $class->GetClassId(),
					'style' => 'width: 100%;'
				),'<em>Create new '.$class->GetName().'</em><br/>'.$class->GetDescription(),
				    $class->GetIcon().'_add'));
			};
		};
	}
}

class modCmsInfo extends cDycmsClientModule
{
	function __construct(){
		parent::__construct(uiGetIcon('server').'Dynamo CMS - Information');
		
		$this->AddChild(<<<END
<h3>Version - Alvin(0.1)</h3>
Developed by <a href="http://www.dysoft.co.uk/studios">Dynamo Studios</a><br/>
<em>Licensed</em> for exclusive non-commercial use.
<p>Components<ul>
<li><em>CMS Core</em> version 0.3.3</li>
<li><em>CMS UI Toolkit</em> version 0.1</li>
<li><em>AJAX Request Manager</em> version 0.1</li>
<li><em>Choqolate WebUI</em> version 0.1</li>
<li><em>Mootools JS Framework</em> version 1.11<br/>
	Used under license (MIT) from
	<a href="http://www.mootools.net">Valerio Proietti</a></li>
<li><em>FCKEditor HTML Editor</em><br/>
	Used under license (GNU Lesser GPL v2.1) from
	<a href="http://www.fckeditor.net">Frederico Caldeira Knabben</a></li>
<li><em>Silk Icon Set</em> version 1.3<br/>
	Used under license (Creative Commons Attribution 2.5) from
	<a href="http://www.famfamfam.com/lab/icons/silk/">Mark James</a></li>
</p>
END
		);
	}
}